package com.qf.j2112.controller;

import com.github.pagehelper.PageInfo;
import com.qf.j2112.entity.SysOrder;
import com.qf.j2112.entity.SysOrderitem;
import com.qf.j2112.entity.User;
import com.qf.j2112.service.AuthorService;
import com.qf.j2112.service.OrdersFeign;
import com.qf.j2112.util.JwtUtil;
import com.qf.j2112.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.ORB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import sun.security.krb5.internal.PAData;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@Slf4j
public class AuthorController {
    @Autowired
    AuthorService authorService;
    @Autowired
    OrdersFeign ordersFeign;
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ActionResult login(@RequestBody User user){
//        log.info("输入"+user);
        User user1 = authorService.findUser(user.getUsername());
//        log.info("输出"+user1);
        ActionResult actionResult = new ActionResult();
        if (ObjectUtils.isEmpty(user1)){
            actionResult.setMsg("用户名不存在");
            actionResult.setStatus(400);
        }else {
            if (user1.getUsername().equals(user.getUsername())&&user1.getPassword().equals(user.getPassword())){
                actionResult.setStatus(200);
                actionResult.setMsg("登录成功！");
                String token = JwtUtil.sign(user.getUsername());
                actionResult.setToken(token);
                user1.setPassword("");
                actionResult.setObj(user1);
            }else{
                actionResult.setMsg("用户名或密码错误！");
                actionResult.setStatus(300);
            }
        }
        return actionResult;
    }
    @RequestMapping(value = "/status",method = RequestMethod.POST)
    public ActionResult statu(@RequestBody OrdersVo ordersVo){
//        log.info("输入"+user);
//        log.info("输出"+user1);

        ordersFeign.updateOrder(Integer.parseInt(ordersVo.getSerial()),ordersVo.getStatus()+1);
        ordersVo.setStatus(ordersVo.getStatus()+1);
        ActionResult actionResult = new ActionResult();
        actionResult.setObj(ordersVo);
        actionResult.setStatus(200);
        actionResult.setMsg("修改状态成功！");
        return actionResult;
    }
    @RequestMapping(value = "/pages",method = RequestMethod.POST)
    public ActionResult page(@RequestBody PageVo page){
        ArrayList<OrderList> orderLists = new ArrayList<OrderList>();
        String userId = JwtUtil.getUserId(page.getToken());
        User user = authorService.findUser(userId);
        Integer authority = user.getAuthority();
        log.info("authority ="+authority);
        int status1=0,status2=0;
        if (authority==1){
            status1=3;
            status2=4;
        }else if (authority==2){
            status1=2;
            status2=5;
        }else if(authority==3){
            status1=1;
            status2=6;
        }
        List<SysOrder> sysOrders = ordersFeign.listorder2(page.getPagesize(), page.getPagenum(), status1, status2);
        for (SysOrder sysOrder : sysOrders) {
            OrderList orderList = new OrderList();
            orderList.setDate(sysOrder.getOrderCreatetime());
            orderList.setStatus(sysOrder.getOrderOrderstatusId());
            orderList.setSerial(sysOrder.getOrderId()+"");
            List<SysOrderitem> items=sysOrder.getOrderitems();
            StringBuilder name=new StringBuilder();
            for (SysOrderitem item:items){
                name.append(item.getOrderGoodsName()+" * "+item.getOrderitemNum()+",");
            }
            orderList.setOrder(name.toString());
            orderLists.add(orderList);
        }
        PageInfo pageInfo = ordersFeign.listorder3(page.getPagesize(), page.getPagenum(), status1, status2);
        long total = pageInfo.getTotal();


        orderLists.add(new OrderList(new Date(),"YZXY04","大棉袄*1",4));

        ActionResult actionResult = new ActionResult();
        actionResult.setObj(orderLists);
        actionResult.setTotal((int)total);
        actionResult.setStatus(200);
        return actionResult;
    }
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public AjaxResult list(){
        AjaxResult list = ordersFeign.list();
        ArrayList<SysOrder> info = (ArrayList)list.get("info");
        for (SysOrder sysOrder : info) {
            System.out.println("sysOrder = " + sysOrder);
        }
        int size = list.size();
        System.out.println("size = " + size);
        return list;
    }

}
